﻿using iTextSharp.text;
using iTextSharp.text.pdf;
using System;

namespace TianYu.Core.Pdf.ContentElement.Text
{
    public class LabelElement : AbstractTextElement
    {
        /// <summary> 是否包含复选框 </summary>
        public bool IsCheckBox { get; set; }

        /// <summary> 复选框是否选中 </summary>
        public bool IsChecked { get; set; }

        /// <summary> 自定义内容 </summary>
        public ContentModel Content { get; set; }

        public override void Draw(PdfTemplateContent pdfTemplateContent)
        {
            var pdfContentByte = pdfTemplateContent.PdfWriter.DirectContent;

            //文本
            var columnText = new ColumnText(pdfContentByte);

            //画矩形 
            base.DrawRectangle(columnText, pdfContentByte, Math.Abs(Content.Size - 12));

            float indentationLeft = 0;

            if (IsCheckBox)
            {
                var partial = Content.RowSpace == 1 ? 5 : 0;
                var rectBorder = new Rectangle(X + Border.PaddingLeft, Y + Border.PaddingBottom + partial, X + 10 - Border.PaddingRight, Y + 10 - Border.PaddingTop + partial- Math.Abs(Content.Size - 12));
                rectBorder.BackgroundColor = BaseColor.RED;
                var checkbox = new RadioCheckField(pdfTemplateContent.PdfWriter, rectBorder, Guid.NewGuid().ToString(), "Yes");
                checkbox.Checked = IsChecked;
                checkbox.CheckType = RadioCheckField.TYPE_CHECK;
                checkbox.BackgroundColor = BaseColor.WHITE;
                checkbox.BorderStyle = PdfBorderDictionary.STYLE_SOLID;
                checkbox.BorderColor = BaseColor.BLACK;
                checkbox.BorderWidth = BaseField.BORDER_WIDTH_THIN;

                pdfTemplateContent.PdfWriter.AddAnnotation(checkbox.CheckField);

                indentationLeft = 10;
            }

            //内容 
            var fontStyle = CommonHelper.GetFontStyle(Content.Style);
            var fontColor = (FontColor > 0) ? CommonHelper.GetBaseColor(FontColor) : BaseColor.BLACK;

            Font family = new Font(CommonHelper.GetFontFamilyPath(Content.Family), Content.Size, fontStyle, fontColor);

            Paragraph paragraph = new Paragraph(Content.RowSpace * CommonHelper.BaseLeading);
            paragraph.SpacingBefore = 1;
            paragraph.Font = family;
            paragraph.Alignment = CommonHelper.GetAlignment(Content.Align);
            paragraph.IndentationLeft = indentationLeft;
            paragraph.Add(Content.Text);

            columnText.AddElement(paragraph);

            columnText.Go();
        }
    }
}
